package at.ac.lbg.media.vis.client.theme.view
{
	import at.ac.lbg.media.vis.client.theme.render.ArtworkRenderer;
	
	import flare.display.TextSprite;
	import flare.flex.FlareVis;
	import flare.util.Colors;
	import flare.vis.controls.HoverControl;
	import flare.vis.controls.PanZoomControl;
	import flare.vis.data.Data;
	import flare.vis.data.DataSprite;
	import flare.vis.events.SelectionEvent;
	import flare.vis.operator.label.Labeler;
	import flare.vis.operator.layout.AxisLayout;
	
	import flash.filters.GlowFilter;
	import flash.text.TextFormat;
	
	import mx.collections.ArrayCollection;
	
	public class LandscapeManager {
		public function LandscapeManager(fV:FlareVis, data:ArrayCollection) {
			
			fV.visualization.data = convertData(data);
			var pz:PanZoomControl = new PanZoomControl();
			pz.attach(fV.visualization);
			
			// define the visual encodings
			fV.visualization.data.nodes.setProperties({
				fillColor: 0x018888ff, // transparent fill to catch mouse hits
				lineWidth: 2
			});
			
			// define x and y fields from which vis should operate
			var field1:String = "origX";
			var field2:String = "origY";
			var layout:AxisLayout = new AxisLayout(field1, field2);
			fV.visualization.operators.add(layout);
			fV.visualization.xyAxes.xAxis.fixLabelOverlap = false; // keep overlapping labels
			fV.visualization.axes.alpha = 0;
			
			// Labels for each artwork
			var labeler:Labeler = new Labeler("title", null, new TextFormat("Arial", "11", "0x336633"), function(item:Artwork):Boolean{ return item.stackCount < 2 }, Labeler.CHILD);
			labeler.xOffset = 10;
			labeler.yOffset = - 20;
			fV.visualization.operators.add(labeler);
			
			// add mouse over
			var hc:HoverControl = new HoverControl(DataSprite, HoverControl.DONT_MOVE, 
				function(e:SelectionEvent):void {
					e.item.filters = [new GlowFilter(0xE6821E, 0.8, 4, 4, 2, 1, true, false)];
					var aw:Artwork = e.item as Artwork;
					var stacks:ArrayCollection = ArtworkRenderer.instance.stacks;
					for each (var st:ArtworkStack in stacks) {
						if (st.displayedArtwork == aw) {
							var label:TextSprite = new TextSprite(st.label);
							label.name = "stacklabel";
							label.textField.background = true;
							label.textField.backgroundColor = Colors.rgba(255, 255, 255);
							label.x = 20;
							label.y = -20
							label.textFormat = new TextFormat("Arial", "11", "0x336633");
							aw.addChild(label);
							break;
						}
					}
//					var label:TextSprite = new TextSprite(Artwork(e.item).title);
//					label.y = -25;
//					e.item.addChild(label);
				}
				, function(e:SelectionEvent):void {
					e.item.filters = null;
					var sp:TextSprite = e.item.getChildByName("stacklabel");
					if (sp != null) {
						e.item.removeChild(sp);
					}
				});
			fV.visualization.controls.add(hc);
			
		}
		
		public static function convertData(artworks:ArrayCollection):Data {
			var data:Data = new Data();
			
			for (var i:int = 0; i < artworks.length; i++) {
				var aw:Artwork = artworks[i];
				aw.renderer = ArtworkRenderer.instance;
				data.addNode(aw);
			}
			return data;
		}

	}
}